﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using EFCoreRepository.Extensions;
using EFCoreRepository.Repositories;
using Microsoft.EntityFrameworkCore;
using Zoomtel.Entity.BaseType;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ.Models;
using Zoomtel.Persist.Query;

namespace Zoomtel.Persist.BIZ
{
    public class ItemPromotionRepository : RepositoryAb<ItemPromotionEntity>
    {
        public ItemPromotionRepository(DefaultDbContext context) : base(context)
        {
        }

        public List<ItemPromotionModel> QueryUnsettleItemList(ItemPromotionQueryModel model)
        {
            var items = DbContext.Set<ItemEntity>();
            var query = (from promotion in dbSet
                         join item in items on promotion.ItemId equals item.ItemId
                         where promotion.SettleFlag == "0" && promotion.ClickType == "20" &&item.PlatForm==model.PlatForm
                         select new ItemPromotionModel
                         {
                             ItemId = item.ItemId,
                             ItemName = item.ItemName
                         }).Distinct().ToList();

            return query;
        }

        public ItemPromotionModel QueryItemPromotionTotalCountInfo(ItemPromotionQueryModel model) {
            var itemPromotionModel = new ItemPromotionModel();
            var promotions = DbContext.Set<ItemPromotionEntity>();
            var items = DbContext.Set<ItemEntity>();
            
            //var visitCountQuey= (from promotion in promotions
            //                     join item in items on promotion.ItemId equals item.ItemId
            //                     where item.PlatForm == model.PlatForm && promotion.SettleFlag == model.SettleFlag
            //                     && item.ItemId == model.ItemId && promotion.ClickType == "10"//第一段
            //                     select new ItemPromotionModel
            //                     {
            //                         BegTime = promotion.ClickTime,
            //                         SettleFlag ="10"
            //                     }).ToList().GroupBy(x => x.SettleFlag).Select(a => new ItemPromotionModel
            //                     {
            //                         VisitCount = a.Count(),
            //                         BegTime = a.Min(x => x.BegTime)
            //                     }).FirstOrDefault();
            var visitCountQuery = dbSet.Where(a => a.ClickType == "10" && a.SettleFlag == model.SettleFlag&&a.ItemId==model.ItemId).ToList();
            var finishCountQuery = dbSet.Where(a => a.ClickType == "20" && a.SettleFlag == model.SettleFlag && a.ItemId == model.ItemId).ToList();

            itemPromotionModel.BegTime = visitCountQuery.Min(a => a.ClickTime);
            itemPromotionModel.EndTime = DateTime.Now;
            if (itemPromotionModel.BegTime.IsNull())
            {
                itemPromotionModel.TimeLen = 0;
            }
            else
            {
                itemPromotionModel.TimeLen = (itemPromotionModel.EndTime - itemPromotionModel.BegTime).TotalHours.ToString("#0.00").ToDecimal();
            }
            
            itemPromotionModel.VisitCount = visitCountQuery.Count();
            itemPromotionModel.FinishCount = finishCountQuery.Count();
            
            return itemPromotionModel;
        }
        public async Task<int> UpdateBatchAsync(ItemPromotionQueryModel model)
        {
            //标记批次，只计算该批次数据
            //string batchcode = DbContext.GetSeq("S_BIZ_BATCH").ToString();
            int count = await DbContext.ExecuteSqlAsync("update T_BIZ_ItemPromotion SET Batch=@batch,SettleFlag='10' where SettleFlag='0' and ItemId=@itemid", new
            
            {
                batch = model.Batch,
                itemid = model.ItemId
            });
            return count;
        }

        public async Task<int> RollBackPromotions(ItemPromotionQueryModel model)
        {
            //标记批次，只计算该批次数据
            //string batchcode = DbContext.GetSeq("S_BIZ_BATCH").ToString();
            int count = await DbContext.ExecuteSqlAsync("update T_BIZ_ItemPromotion SET Batch='NULL',SettleFlag='0' where batch=@batch and ItemId=@itemid", new
            {
                batch = model.Batch,
                itemid = model.ItemId
            });
            return count;
        }

        public List<ItemPromotionModel> QueryAgent(ItemPromotionQueryModel model)
        {
            //根据clicktype取访问次数和完成次数
            var query = (from item in dbSet
                         where item.Batch == model.Batch && item.ItemId == model.ItemId
                         select new ItemPromotionModel
                         {
                             AgentId = item.AgentId,
                             ItemId = item.ItemId

                         }).Distinct().ToList();
            return query;
        }

        public List<ItemPromotionModel> QueryAgentCount(ItemPromotionQueryModel model)
        {
            //根据clicktype取访问次数和完成次数
            var query = (from item in dbSet
                          where item.ClickType == model.ClickType && item.Batch == model.Batch && item.ItemId == model.ItemId
                          group item by new { item.AgentId, item.ItemId } into g
                          select new ItemPromotionModel
                          {
                              ItemId = g.Key.ItemId,
                              AgentId = g.Key.AgentId,
                              //Wid=g.Key.Wid,
                              VisitCount = g.Count()
                              
                          }).ToList();
            return query;
        }
     }
}
